package com.group3.hospital.controller;

import com.group3.hospital.bean.Appointment;
import com.group3.hospital.bean.Doctor;
import com.group3.hospital.bean.Subject;
import com.group3.hospital.service.AppointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 尚硅谷
 * @create 2021/12/30
 */
@Controller
@SessionAttributes(names = {"appointments","ints","subjects","doctor"})
public class AppointmentController {
    @Autowired
    private AppointmentService appointmentService;
    //根据医生姓名查询相关信息
    @RequestMapping("getDoctor")
    public String getDoctor(String docName,Model model){
        System.out.println(docName);
        try {
            Doctor doctor = appointmentService.queryDoctor(docName);
            System.out.println(doctor);
            model.addAttribute("doctor",doctor);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/doctorEveryOneUI";
    }

    @RequestMapping("doctorEveryOneUI")
    public String doctorEveryOneUi(){
        return "/html/doctorEveryOne";
    }



    //前台预约页面
    @RequestMapping("appointmentUi")
    public String appointmentUi(Model model){
        //查询全部科室
        List<Subject> subjects = new ArrayList<>();
        try {
            subjects = appointmentService.queryAllSubject();
        } catch (Exception e) {
            e.printStackTrace();
        }
//        subjects.forEach(subject -> System.out.println(subject));
        model.addAttribute("subjects",subjects);
        //根据科室查询医生姓名
        return "/html/appointment";
    }
    //后台预约页面  第一次进入的界面
    @RequestMapping("appointmentPageUi")
    public String appointmentPageUi(Model model){
        List<Appointment> appointments = new ArrayList<>();
        Integer pages = 0;
        try {
            //得到后台前十个数据
            appointments = appointmentService.queryAll();

            Integer integer = appointmentService.queryCount();
            pages = (integer/10) + 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
//        System.out.println(pages);
        int[] ints = new int[pages];
        for (int anInt : ints) {
            System.out.println(anInt);
        }

        model.addAttribute("ints",ints);
        model.addAttribute("appointments",appointments);
//        appointments.forEach(appointment -> System.out.println(appointment));
        return "/html/appointmentPage";
    }


    //后台预约界面 之后的界面
    @RequestMapping("queryByPage")
    public String queryByPage(Model model,Integer page){
        List<Appointment> appointments = new ArrayList<>();
        Integer pages = 0;
        Integer pageUse = (page-1) * 10;
        try {
            appointments = appointmentService.queryByPage(pageUse);

            Integer integer = appointmentService.queryCount();
            pages = (integer/10) + 1;


        } catch (Exception e) {
            e.printStackTrace();
        }
//        appointments.forEach(appointment -> System.out.println(appointment));
        int[] ints = new int[pages];
        model.addAttribute("ints",ints);
        model.addAttribute("appointments",appointments);
        return "/html/appointmentPage";
    }


    //增加一个预约挂号
    @RequestMapping("addAppointment")
    public String addAppointment(String appointmentDate,String subjectName, String illness, String appointmentName, Long appointmentTel){
        String newDate = appointmentDate.replace("T", " ");
        Date date = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        try {
            date = format.parse(newDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Appointment appointment = new Appointment();
        appointment.setSubjectName(subjectName);
        appointment.setAppointmentIllness(illness);
        appointment.setAppointmentName(appointmentName);
        appointment.setAppointmentTel(appointmentTel);
        appointment.setAppointmentDate(date);
        try {
            appointmentService.addAppointment(appointment);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/appointmentUi";
    }
}
